#!/usr/bin/python

from httplib import HTTPConnection
from random import random
import md5

host = "localhost"
port = 1234
login = "user1"
password = "pass1"
pass_key = ""

def print_resp(resp):
	print
	print resp.status, resp.reason
	print resp.getheaders()
	resp.read()

if __name__ == "__main__":
	con = HTTPConnection(host, port)
	sid = str(random())

	def req_hello():
		con.request("GET", "/", "",
				{
					"action": "hello",
					"login": login,
					"session-id": sid
					})
		resp = con.getresponse()
		print_resp(resp)
		global pass_key
		pass_key = resp.getheader("param")


	def req_auth():
		m = md5.new()
		m.update(password + pass_key)
		param = m.hexdigest()
		con.request("GET", "/", "",
				{
					"action": "auth",
					"login": login,
					"param": param,
					"session-id": sid
					})
		print_resp(con.getresponse())

	def req_alive():
		con.request("GET", "/", "",
				{
					"action": "alive",
					"login": login,
					"session-id": sid
					})
		print_resp(con.getresponse())

	req_hello()
	req_auth()
	req_alive()
